//添加
layui.use(['layer','laydate','form','jquery','upload','element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var form = layui.form;
    var upload = layui.upload;
    var element = layui.element;

    laydate.render({
        elem: '#test1',
        type: 'datetime'
    });

    $("#body").mouseover(function move() {
        getCourseClass2
    });
    
    
    getCourseClass2()
    function getCourseClass2() {
        //var courseIntro=  $("name=courseType").val();
        var typeNum= localStorage.getItem("classId");
        console.log(typeNum);
        $.ajax({
            url: '/courseTable/getCourseClass',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var opt="<select name='classId' id='upclassId'>";
                opt+="<option value=''>请选择课程分类</option>";
                for(var da in data.data) {
                    var ba=da;
                    da++;
                    if(data.data[ba].categroyId==typeNum){
                        opt +="<option value='"+da+ "'selected>"+data.data[ba].title+"</option>"
                    }else{
                        opt +="<option value='"+da+ "'>"+data.data[ba].title+"</option>"
                    }
                }
                opt+="</select>"
                $("#sot").html(opt);
                //刷新下拉框
                form.render('select');
            }
        });
    }


    Refresh = function Refresh(){

        var createDate = $('input[name=executionTime]').val();
        console.log(createDate);

        var classification = document.getElementById("upclassId");
        var indexClass = classification.selectedIndex;
        var classId = classification.options[indexClass].value;
        console.log(classId);

        var courseName=$("#productCode").val();
        var userName= $("#currentpriceProduct").val();
        var title= $("#xiaoliang").val();
        var courseNprice= $("#title").val();
        var likeNum= $("#productLabel").val();
        var collectNum= $("#hits").val();
        var courseIntro=  $("#productDescription").val();
        var courseInfo= $("#beizhu").val();
        var url=localStorage.getItem("url");
        var courseUrl=localStorage.getItem("courseUrl");
        var userName=localStorage.getItem("userName");
        var userAcc=localStorage.getItem("userAcc");
        var courseId=localStorage.getItem("updateCourseId");
        var coursePrice=localStorage.getItem("coursePrice");
        var courseStatus=localStorage.getItem("courseStatus");
        var courseCstatus=localStorage.getItem("courseCstatus");

        var timestamp = new Date(createDate).getTime();
        document.write(timestamp);

        // 根据毫秒数构建 Date 对象
        var date = new Date(timestamp);
        // 格式化日期
        //createDate = date.toLocaleString();
        var userId=localStorage.getItem("createId");
        getTable();
        function getTable() {
            var courseId= localStorage.getItem("courseId");
            console.log(courseId);
            var jsCourse = {
                'courseId' : courseId,
                'courseName': courseName,
                'createDate':createDate,
                'title ': title,
                'courseNprice': courseNprice,
                'likeNum': likeNum,
                'collectNum': collectNum,
                'courseIntro': courseIntro,
                'courseInfo': courseInfo,
                'userName': userName,
                'url': url,
                'courseUrl': courseUrl,
                'userName': userName,
                'userAcc': userAcc,
                'courseId':courseId,
                'updateId':userId,
                'classId':classId,
                'coursePrice':coursePrice,
                'courseStatus':courseStatus,
                'courseCstatus':courseCstatus
            }
            console.log(jsCourse);
            $.ajax({
                url: '/courseTable/updateCourse',
                type: 'post',
                dataType: 'json',
                data: jsCourse,
                success: function (data) {
                    if(data.data==true){
                        layer.alert("修改成功");
                        parent.location.reload();
                    }else{
                        layer.alert("修改失败");
                        parent.location.reload();
                    }
                }
            });
        }
    }

    //上传封面
    var uploadInst = upload.render({
        elem: "#url",
        url: "/courseTable/upFile",
        multiple: true,
        field: "files",
        accept: "images",
        before: function (obj) {
            $('#images').show();
            obj.preview(function (index, file, result) {
                console.log(result);
            });
        },
        done: function (res, index, upload) {
            var mainImage = res.data;
            console.log(mainImage);
            console.log(mainImage.path);
            var url='视频预览:<video src="'+mainImage.path+'"width="200px" height="200px" type="video/mp4" controls autoplay '+'border="1px solid #ccc" style="margin-left: 50px"></video>'
            $("#video").html(url);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    //上传视频
    var uploadInst = upload.render({
        elem: "#course_url",
        url: "/courseTable/uploadFile",
        multiple: true,
        field: "files",
        accept: "video",
        before: function (obj) {
            $('#images').show();
            obj.preview(function (index, file, result) {
                console.log(result);
            });
        },
        done: function (res, index, upload) {
            var mainImage = res.data;
            console.log(mainImage);
            console.log(mainImage.path);
            var url='视频预览:<video src="'+mainImage.path+'"width="200px" height="200px" type="video/mp4" controls autoplay '+'border="1px solid #ccc" style="margin-left: 50px"></video>'
            $("#video").html(url);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    //取消
    closeWode = function closeWode(){
        parent.location.reload();
    }


})